package henu.javaweb.dao;

import java.net.URLEncoder;
import java.sql.*;
import java.util.ArrayList;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.javaweb.action.ITeacherDao;
import henu.javaweb.bean.Teacher;
import henu.javaweb.util.WebDbPool;

import javax.servlet.http.HttpServletRequest;

public class TeacherDao implements ITeacherDao{

	private QueryRunner queryRunner=new QueryRunner();
	public Teacher login(String tid,String tpassword){
		// TODO Auto-generated method stub

			Teacher t=null;
			String sql = "SELECT * FROM tb_teacher WHERE t_id = ? AND t_password = ?";
			try {
				PreparedStatement ps = WebDbPool.executePreparedStatement(sql);
				ps.setString(1, tid);
				ps.setString(2, tpassword);
				ResultSet rs = ps.executeQuery();         //执行查询操作
				if(rs.next()) {
					t=new Teacher();
					t.setT_id(rs.getString("t_id"));
					t.setT_username(rs.getString("t_username"));
					t.setT_password(rs.getString("t_password"));
					t.setT_realname(rs.getString("t_realname"));
					t.setT_faculty(rs.getString("t_faculty"));
					t.setT_major(rs.getString("t_major"));
					t.setT_isAdmin(rs.getBoolean("t_isAdmin"));
				}		
			}catch(SQLException e) {
				e.printStackTrace();
			}
			WebDbPool.close();
			return t;
	}
	public boolean Admin() {
		int flag=0;
		String sql="select count(*) as num from tb_teacher where t_isAdmin=1";
		try {
			PreparedStatement ps=WebDbPool.executePreparedStatement(sql);
			ResultSet rs= ps.executeQuery();
			if(rs.next()) {
				flag=rs.getInt("num");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		if(flag==0) {
			return false;
		}else {
			return true;
		}
		
	}
	public int update(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String number=null;
		if(request.getParameter("tid")!=null) number=request.getParameter("tid");
		String sql = "update tb_teacher set t_id=?, "+
				"t_username=?, "+
				"t_password=?, "+
				"t_realname=?, "+
				"t_isAdmin=?, "+
				"t_faculty=?, "+
				"t_major=? "+
				"where t_id = "+number+";";


		try {
			// 预编译
			PreparedStatement ps = WebDbPool.executePreparedStatement(sql);
			ps.setString(1, request.getParameter("tid"));
			if(request.getParameter("tuser")!=null) ps.setString(2, request.getParameter("tuser"));
			else ps.setString(2, null);
			if(request.getParameter("password")!=null) ps.setString(3, request.getParameter("password"));
			else ps.setString(3, null);
			if(request.getParameter("tfullname")!=null) ps.setString(4, request.getParameter("tfullname"));
			else ps.setString(4, null);

			String admin1=request.getParameter("admin");
			String admin;
			if(admin1==null) {
				admin = "0";
			}else {
				admin = "1";
			}

			if(request.getParameter("admin")!=null) ps.setString(5, admin);
			else ps.setString(5, admin);
			if(request.getParameter("faculty")!=null) ps.setString(6, request.getParameter("faculty"));
			else ps.setString(6, null);
			if(request.getParameter("major")!=null) ps.setString(7, request.getParameter("major"));
			else ps.setString(7, null);

			return ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Teacher findByTid(String tid) {
		// TODO Auto-generated method stub
		Teacher teacher=null;
		String sql="select * from tb_teacher where t_id='"+tid+"'";
		try {
			teacher=queryRunner.query(WebDbPool.getConnection(), sql, new BeanHandler<Teacher>(Teacher.class));
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		return teacher;
	}
	@Override
	public ArrayList<Teacher> getAllTeacher() {
		ArrayList<Teacher> teachers=new ArrayList<>();
		String sql="SELECT * FROM tb_teacher";
		try {
			teachers=(ArrayList<Teacher>) queryRunner.query(WebDbPool.getConnection(), sql, new BeanListHandler<Teacher>(Teacher.class));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teachers;
	}


	@Override
	public int add(HttpServletRequest request) {
		String tid=request.getParameter("tid");
		String tuser=request.getParameter("tuser");
		String tpass=request.getParameter("tpass");
		String tfullname=request.getParameter("tfullname");
		String college=request.getParameter("college");

		String major=request.getParameter("major");
		String admin1=request.getParameter("admin");

		System.out.println(admin1);

		String admin;
		if(admin1==null) {
			admin = "0";
		}else {
			admin = "1";
		}
		System.out.println(tid);
		System.out.println(tuser);
		System.out.println(tpass);
		System.out.println(tfullname);
		System.out.println(college);
		System.out.println(major);
		System.out.println(admin);

		String sql="INSERT INTO tb_teacher(t_id,t_username,t_password,t_realname,t_faculty,t_major,t_isAdmin)"+"VALUES(?,?,?,?,?,?,?)";
		try {
			PreparedStatement ps = WebDbPool.executePreparedStatement(sql);
			int result = 0;
			ps.setString(1, tid);
			ps.setString(2, tuser);
			ps.setString(3, tpass);
			ps.setString(4, tfullname);
			ps.setString(5, college);
			ps.setString(6, major);
			ps.setString(7, admin);
			result = ps.executeUpdate();
			ps.close();
			WebDbPool.close();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public boolean updateStuPass(String t_id,String spass) {
		// TODO Auto-generated method stub
		String sql="update tb_teacher set t_password=? where t_id=?";
		int result=0;
		try {
			PreparedStatement ps=WebDbPool.executePreparedStatement(sql);
			ps.setString(1, spass);
			ps.setString(2, t_id);
			result=ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result>0;
	}
	@Override
	public Teacher findById(String tid) {
		Teacher teacher=null;
		String sql="select * from tb_teacher where t_id='"+tid+"'";
		try {
			teacher=queryRunner.query(WebDbPool.getConnection(), sql, new BeanHandler<Teacher>(Teacher.class));
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		return teacher;
	}
}


